package com.ibm.law.ui.system.setup;

import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TableViewerEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.ibm.law.domain.Dictionary;
import com.ibm.law.domain.DictionaryList;
import com.ibm.law.service.system.DictionaryService;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.viewers.ViewerProperties;
import org.eclipse.core.databinding.beans.PojoProperties;

import com.ibm.law.ui.common.CloseDialogSelectionAdapter;
import com.ibm.law.ui.common.RowNumberLabelProvider;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.jface.databinding.viewers.CellEditorProperties;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.core.databinding.beans.IBeanValueProperty;
import org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport;

public class DictionarySetup extends Composite {
	private DataBindingContext m_bindingContext;
	private Table table;
	private java.util.List<Dictionary> rootDictionaries;
	private DictionaryService dictionaryService = new DictionaryService();
	private DictionaryList dictionaryList = new DictionaryList();
	private ListViewer listViewer;
	private TableViewer tableViewer;
	private TableViewerColumn tableViewerColumn;
	private Dictionary currentDictionary;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public DictionarySetup(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(2, false));
		rootDictionaries = dictionaryService.getRootDictionaries();
		listViewer = new ListViewer(this, SWT.BORDER | SWT.V_SCROLL);

		List list = listViewer.getList();
		list.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) listViewer
						.getSelection();
				currentDictionary = (Dictionary) selection.getFirstElement();
				System.out.println(currentDictionary.getId());
				dictionaryList.setList(dictionaryService
						.getSubDictionaries(currentDictionary));
			}
		});
		GridData gd_list = new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1);
		gd_list.minimumWidth = 200;
		list.setLayoutData(gd_list);

		tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
				tableViewer) {
			protected boolean isEditorActivationEvent(
					ColumnViewerEditorActivationEvent event) {
				return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
						|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
						|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
			}
		};

		TableViewerEditor.create(tableViewer, actSupport,
				ColumnViewerEditor.TABBING_HORIZONTAL
						| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
						| ColumnViewerEditor.TABBING_VERTICAL
						| ColumnViewerEditor.KEYBOARD_ACTIVATION);
		tableViewerColumn = new TableViewerColumn(tableViewer, SWT.RIGHT);
		tableViewerColumn.setLabelProvider(new RowNumberLabelProvider());
		TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
		tblclmnNewColumn.setResizable(false);
		tblclmnNewColumn.setWidth(340);
		tblclmnNewColumn.setText("New Column");

		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, false,
				false, 2, 1));
		RowLayout rl_composite = new RowLayout(SWT.HORIZONTAL);
		rl_composite.justify = true;
		composite.setLayout(rl_composite);

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Dictionary dictionary = new Dictionary();
				dictionary.setParent(currentDictionary);
				dictionaryList.add(dictionary);
				tableViewerColumn.getViewer().editElement(dictionary, 0);
			}
		});
		btnNewButton.setText("增加");

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) tableViewer
						.getSelection();
				Dictionary dictionary = (Dictionary) selection
						.getFirstElement();
				dictionaryService.delete(dictionary);
				dictionaryList.remove(dictionary);
			}
		});
		btnNewButton_1.setText("删除");

		Button btnNewButton_2 = new Button(composite, SWT.NONE);
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dictionaryService.saveAll(dictionaryList.getList());
			}
		});
		btnNewButton_2.setText("保存");

		Button btnNewButton_3 = new Button(composite, SWT.NONE);
		btnNewButton_3.addSelectionListener(new CloseDialogSelectionAdapter());
		btnNewButton_3.setText("退出");
		m_bindingContext = initDataBindings();
		listViewer.getList().select(0);
		listViewer.getList().notifyListeners(SWT.Selection, null);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap observeMap = BeansObservables.observeMap(
				listContentProvider.getKnownElements(), Dictionary.class,
				"name");
		listViewer.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		listViewer.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(Dictionary.class)
				.observe(rootDictionaries);
		listViewer.setInput(selfList);
		//
		ObservableListContentProvider listContentProvider_1 = new ObservableListContentProvider();
		IObservableMap observeMap_1 = BeansObservables.observeMap(
				listContentProvider_1.getKnownElements(), Dictionary.class,
				"name");
		tableViewer.setLabelProvider(new ObservableMapLabelProvider(
				observeMap_1));
		tableViewer.setContentProvider(listContentProvider_1);
		//
		IObservableList listDictionaryListObserveList = BeanProperties.list(
				"list").observe(dictionaryList);
		tableViewer.setInput(listDictionaryListObserveList);
		//
		CellEditor cellEditor = new TextCellEditor(tableViewer.getTable());
		IValueProperty cellEditorProperty = CellEditorProperties.control()
				.value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty = BeanProperties.value("name");
		tableViewerColumn.setEditingSupport(ObservableValueEditingSupport
				.create(tableViewer, bindingContext, cellEditor,
						cellEditorProperty, valueProperty));
		//
		return bindingContext;
	}
}
